package com.turing.selfwebservice.cardholderuser;

import java.io.File;
import java.math.BigInteger;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.ws.Service;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.turing.post.webservice.client.CardHolderUser;
import com.turing.post.webservice.client.CommonWebResult;
import com.turing.post.webservice.client.WebWscImpl;
import com.turing.post.webservice.client.WebWscImplPortType;
import com.turing.selfwebservice.system.SessionHashTable;
import com.turing.selfwebservice.util.ServletUtil;

/**
 * 注册用户Servlet注册、改密
 * 
 * @author 李治
 * 
 */
public class CardHolderUserPreServlet extends DispatchAction {
	private HttpSession session = null;
	private WebWscImplPortType postPortType = null;


	/**
	 * 持卡用户注册
	 */
	public ActionForward RegisterCardHolderUser(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		session = request.getSession();
		try {
			getLink();
			SessionHashTable sht = (SessionHashTable) session
					.getAttribute("SessionHashTable");
			postPortType = sht.getWebservice();
			String logonName = getParameter(request, "logonName");// 登录名
			String logonPasswd = getParameter(request, "logonPasswd");// 密码
			String email = getParameter(request, "email");// 邮箱
			String name = getParameter(request, "name");// 姓名
			String idTypeId = getParameter(request, "IdType");// 证件类型
			String idNo = getParameter(request, "idNo");// 证件号
			String sex = getParameter(request, "sex");// 性别
			String cardNo = getParameter(request, "cardNo");// 卡号
			String cardPwd = getParameter(request, "cardPwd");// 卡密码
			String verifyCode = getParameter(request, "verifyCode");
			if (ServletUtil.isEmpty(verifyCode)) {
				response.getWriter().write("验证码输入错误");
			}
			CardHolderUser cardHolderUser = new CardHolderUser();
			cardHolderUser.setLogonName(logonName);
			cardHolderUser.setLogonPasswd(logonPasswd);
			cardHolderUser.setEmail(email);
			cardHolderUser.setName(name);
			cardHolderUser.setIdTypeId(new BigInteger(idTypeId));
			cardHolderUser.setIdNo(idNo);
			cardHolderUser.setSex(sex);
			CommonWebResult commonWebResult = postPortType
					.insertCardHolderUser(cardHolderUser, cardNo, cardPwd);
			if (commonWebResult.isSuccess()) {
				response.getWriter().write("true");
			} else {
				response.getWriter().write(commonWebResult.getExcepMessage());
			}
		} catch (Exception e) {
			request.setAttribute("Error",
					"webservice连接异常，检查webservice是否启动或网络连通!");
			response.getWriter().write("webservice连接异常，检查webservice是否启动或网络连通!");
		}
		return null;
	}

	private void getLink() throws Exception {
		String wsdl = getDbConnection();
		if (!wsdl.equals("")) {
			URL wsdlURL = new URL(wsdl);
			QName serviceQName = new QName(
					"http://impl.webservice.post.turing.com/", "WebWscImpl");
			Service service = WebWscImpl.create(wsdlURL, serviceQName);
			WebWscImplPortType postPortType = service
					.getPort(WebWscImplPortType.class);
			SessionHashTable sht = new SessionHashTable();
			sht.setWebservice(postPortType);
			session.setAttribute("SessionHashTable", sht);
		}
	}

	/**
	 * 读取连接数据库配置文件
	 * 
	 * @return string
	 * @throws Exception
	 */
	private String getDbConnection() throws Exception {
		String configPath = Thread.currentThread().getContextClassLoader()
				.getResource("/").getPath();
		String connStr = "";
		File f = new File(configPath + "/DbConnection.xml");
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.parse(f);
		NodeList nl = doc.getElementsByTagName("VALUE");
		for (int i = 0; i < nl.getLength(); i++) {
			connStr = doc.getElementsByTagName("connStr").item(i)
					.getFirstChild().getNodeValue();
		}
		return connStr;
	}

	private String getParameter(HttpServletRequest request, String value)
			throws Exception {
		String val = request.getParameter(value);
		if (StringUtils.isBlank(val)) {
			return null;
		}
		return val.trim();
	}
}
